<?php
/* 
* @Author: Administrator
* @Date:   2014-07-18 00:00:22
* @Last Modified by:   Administrator
* @Last Modified time: 2014-07-18 00:02:44
*/

class BaseinfoModel extends Model{
	public $table = 'baseinfo';

	//定义验证规则
	public $validate = array(
		array('realname','nonull','真实姓名不能为空',2,3),
		array('realname','maxlen:20','请填写正确的姓名',2,3),
		array('realname','minlen:2','请填写正确的姓名',2,3),
		array('sex','num:1,2','性别选择错误',2,3),
		array('birthday','regexp:/^\d{4}-\d{2}-\d{2}$/','生日格式错误',3,3),
		array('peopleaddress','nonull','通讯地址不能为空',2,3),
		array('peopleaddress','maxlen:255','通讯地址不能超过255个字符',2,3),
		array('peoplenumber','tel','固定电话格式不正确',3,3),
		array('qq','regexp:/^[1-9]\d{4,10}$/','qq号码格式不正确',3,3),
		array('msn','email','msn格式不正确',3,3),
		array('peopleintro','maxlen:500','个人简介不能超于500个字符',3,3),
		);

	public $auto = array(
		array('user_uid','_uid','method',2,1)
		);

	public function _uid(){
		return session('uid');
	}
	/**
	 * [add_data 添加数据]
	 */
	public function add_data(){
		$this->add();
	}
	/**
	 * 判断是否该用户有基本信息
	 * @return [type]
	 */
	public function have_info()
	{
		return $this->where(array('user_uid'=>session('uid')))->getField('pbid');
	}
	/**
	 * 获取信息
	 * @return [type]
	 */
	public function get_info($field=NULL)
	{
		return $this->field($field)->where(array('user_uid'=>session('uid')))->find();
	}

	public function update_info()
	{
		return $this->where(array('user_uid'=>session('uid')))->update();
	}



	public function update_face()
	{
		$upload = new Upload();
		$uploadFiles = $upload->upload();
		// p($uploadFiles);
		if (!$uploadFiles) {
			echo $upload->error;
			$this->error = $upload->error;
			return false;
		}
		// echo 11111;
		$where = array('user_uid'=>session('uid'));
		#获取更新前缩略图片路径
		$oldFile = ROOT_PATH.'/'.$this->where($where)->getField('face');
		// echo $oldFile;
		#更新图片路径
		$this->where($where)->update(array('face'=>$uploadFiles[0]['thumb']));

		if (is_file($oldFile)) {
			unlink($oldFile);
			#获取原图的路径
			$oldImages = str_replace(C('THUMB_ENDFIX').'.','.',$oldFile);
			if ($oldImages) unlink($oldImages);
		}

	}
}
?>
